//
//  UIImageView+LF.m
//  AntFarms
//
//  Created by LF on 2017/2/23.
//  Copyright © 2017年 GG. All rights reserved.
//

#import "UIImageView+LF.h"

@implementation UIImageView(LF)

- (void)showInMiddle
{
    [self setContentScaleFactor:[[UIScreen mainScreen] scale]];
    self.contentMode =  UIViewContentModeScaleAspectFill;
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.clipsToBounds  = YES;
    self.layer.masksToBounds = YES;
}

- (void) lf_setImageWithString:(id )picString forType:(PlaceHolderType)type
{
    NSURL *picURL;
    if ([picString class] == [NSURL class]) {
        picURL = picString;
    } else if ([picString isKindOfClass:[NSString class]]){
        picURL = [NSURL URLWithString:picString];
    }
    
    NSString *placeHolderImgName;
//    NSString *errorImgName;
    switch (type) {
        case PlaceHolderTypeNormal: // 0
        {
            placeHolderImgName = @"ic_placeholder";
        }
            break;
        case PlaceHolderTypeBig:  // 1
        {
            placeHolderImgName = @"ic_placeholder_big";
        }
            break;
        case PlaceHolderTypeAvatar: // 2
        {
            placeHolderImgName = @"ic_default_avatar";
        }
            break;
        case PlaceHolderTypeSpecial: // 3
        {
            placeHolderImgName = @"";
        }
            break;

            
        default:
            break;
    }
    
    [self sd_setImageWithURL:picURL placeholderImage:[UIImage imageNamed:placeHolderImgName] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (error) {
            self.image = [UIImage imageNamed:placeHolderImgName];
        }
    }];
}

@end
